package exttable.testversioning.tablemodel;

import dbutils.DbUtils;
import dbutils.StringQueries;
import exttable.testversioning.IDBUtils;
import exttable.testversioning.TestVersioningDBUtils;
import exttable.timetable.ETestType;
import exttable.view.ViewAnalysis;
import exttable.view.ViewAnalysisVersionPhase;
import exttable.view.ViewVersion;
import utils.ELocale;
import utils.Localisation;

import javax.swing.table.AbstractTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jun 15, 2010
 * Time: 5:18:49 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestVersioningModel extends AbstractTableModel {

    private Vector<ViewAnalysisVersionPhase> data = null;
    private static final int ANALYSIS = 0;
    private static final int PRESENT = 1;
    private static final int VERSION = 2;
    private static final int COLUMN_COUNT = 3;
    private IDBUtils dataService = new TestVersioningDBUtils();
    private boolean[] editableColumns = new boolean[3];


    public TestVersioningModel(boolean col0, boolean col1, boolean col2, ETestType type, boolean include) {
        data = getData(type, include);
        editableColumns[0] = col0;
        editableColumns[1] = col1;
        editableColumns[2] = col2;
    }

    public TestVersioningModel(Vector<ViewAnalysisVersionPhase> result) {
        data.addAll(result);
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case ANALYSIS:
                return Localisation.getMessage("table.column.analys");
            case PRESENT:
                return Localisation.getMessage("table.column.present");
            case VERSION:
                return Localisation.getMessage("table.column.version");
            default:
                throw new IllegalArgumentException("no such value");
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case ANALYSIS:
                return data.get(rowIndex).getAnalysis();
            case PRESENT:
                return data.get(rowIndex).isPresent();
            case VERSION:
                return data.get(rowIndex).getVersion();
            default:
                throw new IllegalArgumentException("no such value");
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case PRESENT:
                data.get(rowIndex).setPresent((Boolean) aValue);
                break;
            case VERSION:
                data.get(rowIndex).setVersion((ViewVersion) aValue);
                break;
        }

        fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return editableColumns[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case ANALYSIS:
                return String.class;
            case PRESENT:
                return Boolean.class;
            case VERSION:
                return String.class;
            default:
                throw new IllegalArgumentException("No such value");
        }
    }

    private Vector<ViewAnalysisVersionPhase> getData(ETestType type, boolean include) {
        Vector<ViewAnalysisVersionPhase> toReturn = new Vector<ViewAnalysisVersionPhase>();
        try {
            Connection c = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = c.prepareStatement(StringQueries.ANALYSIS_VERSIONS + addQuery(type, include));
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ViewAnalysisVersionPhase vavp = new ViewAnalysisVersionPhase();
                ViewAnalysis va = new ViewAnalysis.Build()
                        .analysId(rs.getInt("dail.analysis_instr_invest_list_id"))
                        .nameEn(rs.getString("name_en"))
                        .nameKz(rs.getString("name_kk"))
                        .nameRu(rs.getString("name_ru"))
                        .build();
                vavp.setAnalysis(va);
                vavp.setPresent(false);
                dataService.getAmountOfVersions(va);
                ViewVersion vv = new ViewVersion();
                if (rs.getString("tpu.analysis_instr_invest_list_id") != null) {
                    vv.setVersionId("0");
                    vv.setNameEn(Localisation.getMessage("info.versionamount", ELocale.EN) + " " + dataService.getAmountOfVersions(va));
                    vv.setNameRu(Localisation.getMessage("info.versionamount", ELocale.RU) + " " + dataService.getAmountOfVersions(va));
                    vv.setNameKz(Localisation.getMessage("info.versionamount", ELocale.KZ) + " " + dataService.getAmountOfVersions(va));
                } else {
                    vv.setVersionId("-1");
                    vv.setNameEn(Localisation.getMessage("info.noversions", ELocale.EN));
                    vv.setNameRu(Localisation.getMessage("info.noversions", ELocale.RU));
                    vv.setNameKz(Localisation.getMessage("info.noversions", ELocale.KZ));
                }
                vavp.setVersion(vv);
                toReturn.add(vavp);
            }
            c.close();
            return toReturn;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private String addQuery(Enum type, boolean include) {
        if (include) {
            return " where dail.test_type_id = '" + type.name()+"'";
        } else {
            return "";
        }

    }
}
